package com.jdbc.dbutils.test;

import com.jdbc.entity.DogVo;
import com.jdbc.entity.Types;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wzm
 * @create 2023-09-18 15:22
 * project_name:javaweb2308
 */
public class DogHandler<T> implements ResultSetHandler<T> {
    @Override
    public T handle(ResultSet resultSet) throws SQLException {
        //手动处理查询后的值
        List<DogVo> list = new ArrayList<>();
        DogVo dogVo = null;
        while (resultSet.next()){
            dogVo = new DogVo();
            //dog
            dogVo.setId(resultSet.getInt("id"));
            dogVo.setName(resultSet.getString("name"));
            dogVo.setAge(resultSet.getInt("age"));
            dogVo.setSex(resultSet.getString("sex"));

            //type
            int type_id = resultSet.getInt("type_id");
            String type_name = resultSet.getString("type_name");
            dogVo.setTypes(new Types(type_id,type_name));
            list.add(dogVo);
        }
        System.out.println("list = " + list);
        return (T)list;

    }
}
